package com.vickw.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.vickw.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description：
 * @Author VickW
 * @Date 2022/3/20 16:56
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMerhod")
public class OrderHystirxController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("************result: " + result);
        return result;
    }


    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "order_PaymentInfo_TimeOutHandler",
        commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")})
    // @HystrixCommand // 什么都不写，默认用的是全局的降级方法，但是不写这个注解表示该方法没有降级方法，也不会用到全局降级方法
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        // int a = 10/0;
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("************result: " + result);
        return result;
    }
    public String order_PaymentInfo_TimeOutHandler(@PathVariable("id") Integer id){
        return "我是消费者80，对方支付系统繁忙，请稍后再试或者检查一下自己有没有报错或异常信息~~~~~~~";
    }

    public String payment_Global_FallbackMerhod(){
        return "hystrix_order80的Global全局服务降级方法，异常处理信息，请稍后再试~~~~~~~~~~";
    }

}
